
import caller
import time

class WebElement:
    def __init__(self, elementref, pageref, cr):
        self.ref = elementref
        self.pageref = pageref
        self.cr = cr
        
    def isFailed(self):
        return self.cr.isFailed()
        
    def getChildsByCssSelector(self, query):
        args = [self.ref, query]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_VOID_GETCHILDSBYCSSSELECTOR, args)
        elements = []
        childs = self.cr.getReturnArrayValue(ret)
        for ref in childs:
            elements.append(WebElement(ref, self.pageref, self.cr))
        return elements

    def getTagName(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETTAGNAME_LONG_REF, args)
        return self.cr.getReturnValue(ret)

    def getAttribute(self, name):
        args = [self.ref, name]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETATTRIBUTE_LONG_REF_STRING_NAME, args)
        return self.cr.getReturnValue(ret)

    def setAttribute(self, name, value):
        args = [self.ref, name, value]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_VOID_SETATTRIBUTE, args)
        return self.cr.getReturnValue(ret)

    def removeAttribute(self, name):
        args = [self.ref, name]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_VOID_REMOVEATTRIBUTE, args)
        return self.cr.getReturnValue(ret)

    def getProperty(self, name):
        args = [self.ref, name]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETPROPERTY_LONG_REF_STRING_NAME, args)
        return self.cr.getReturnValue(ret)

    def getText(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETTEXT_LONG_REF, args)
        return self.cr.getReturnValue(ret)

    def getValue(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETVALUE_LONG_REF, args)
        return self.cr.getReturnValue(ret)

    def sendKeys(self, typestring, keys):
        args = [self.ref, typestring, keys, 0]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SENDKEYS_LONG_REF_STRING_TYPE_LONG_KEYS, args)
        return self.cr.getReturnNumValue(ret)

    def type(self, val):
        args = [self.ref, val]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_TYPE_LONG_REF_STRING_VAL, args)
        return self.cr.getReturnNumValue(ret)

    def submit(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SUBMIT_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def click(self):
        args = [self.ref, 0]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_CLICK_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def reset(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_RESET_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def toggle(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_TOGGLE_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def focus(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_FOCUS_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def selected(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SELECTED_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def setSelected(self, selected):
        args = [self.ref, selected]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SETSELECTED_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def selectOptionByIndex(self, index):
        args = [self.ref, str(index)]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SELECTOPTIONBYINDEX_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def checked(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SETSELECTED_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def setChecked(self, checked):
        args = [self.ref, checked]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_LONG_SETCHECKED_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)
    
    def isVisible(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_BOOL_ISVISIBLE_LONG_REF, args)
        return self.cr.getReturnNumValue(ret)

    def getRect(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_RECT_GETRECT_LONG_REF, args)
        return self.cr.getReturnObjValue(ret)

    def getInnerXml(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETINNERXML_LONG_REF, args)
        return self.cr.getReturnValue(ret)

    def getOuterXml(self):
        args = [self.ref]
        ret = self.cr.call(self.pageref, caller.Function.WEBELEMENT_STRING_GETOUTERXML_LONG_REF, args)
        return self.cr.getReturnValue(ret)
    
    def getImageBuffer(self):
        args = [self.ref]
        result = self.cr.call(self.pageref, caller.Function.WEBPAGE_STRING_RENDER_IMAGE_BASE64, args)
        buf = self.cr.getReturnValue(result)
        return buf

    def waitForInnerXml(self, times):
        if self.isFailed():
            return False
        count = times
        while count:
            if self.getInnerXml() == "":
                time.sleep(0.2)
                count -= 1
            else:
                return True
        return False
    
    def waitForChildsCssSelector(self, query, times):
        if self.isFailed():
            return False
        count = times
        while count:
            if len(self.getChildsByCssSelector(query)) <= 0 :
                time.sleep(0.2)
                count -= 1
            else:
                return True
        return False

